function addCart() {
	var entityId=jq("input[name='entityId']").val(); //获取商品ID
	var quantity=jq("input[name='quantity']").val(); //获取商品数量
	addCartItemToCart(entityId,quantity);
}
function addCartItemToCart(entityId,quantity) {
	alert(contextPath);
	jq.ajax({
		url:contextPath+"/pre/CartServlet",
		method:"post",
		data:{
			action:"add",
			entityId:entityId,
			quantity:quantity
		},
		dataType:"text",
		success:function(result){
			alert(result);
			//状态进行判断
			if(result=="成功"){
				//执行刷新操作
				ref();
			}
		}
	});
}

function ref() {
	jq("#TestOne").load("../pre/CartServlet","action=f5");
}

function del(entityId){
	jq.ajax({
		url:contextPath+"/pre/CartServlet",
		method:"post",
		data:{
			action:"update",
			entityId:entityId,
			quantity:0
		},
		dataType:"text",
		success:function(jsonStr){
			if(jsonStr=="成功"){
				//重新布局购物车
				setBuyCar();
			}else{
				alert(jsonStr);
			}
		}
	});
}
//加载购物车
function setBuyCar(){
	jq("#BuyCar").load("../pre/CartServlet","action=setBuyCar");
}
//传入this对象 获取数量
function getCount(obj){
	return obj.parent().find(".car_ipt").val();
}
//单击加按钮
function addQuantity(obj,entityId,stock){
	var quantity = Number(getCount(obj))+1;
	//alert(quantity);
	if(stock<quantity){
		alert("商品数量不足");
		return;
	}
	//进行商品数量的修改
	modifyCart(entityId,quantity,obj);
}
//单击减按钮
function subQuantity(obj,entityId){
	var quantity=getCount(obj)-1;
	if(quantity==0){
		alert("数量为1，无法减少，如需删除商品请单击右侧删除按钮")
		quantity=1;
	}
	//进行数量的修改
	modifyCart(entityId,quantity,obj);
}
//修改购物车商品的数量
function modifyCart(entityId,quantity,obj){
	jQuery.ajax({
		url:contextPath+"/pre/CartServlet",
		method:"post",
		data:{
			action:"update",
			entityId:entityId,
			quantity:quantity
		},
		dataType:"text",
		success:function(jsonStr){
			if(jsonStr=="成功"){
				obj.parent().find(".car_ipt").val(quantity);
				setBuyCar();
			}else{
				alert(jsonStr);
			}
		}
	});	
} 

//提交
function submit(){
	var addressId=jq("input[name='selectAddress']:checked").val();
	var newAddress=jQuery("input[name='address']").val();
	var newRemark=jQuery("input[name='remark']").val();
    if(addressId=="" || addressId==null){
        alert("请选择收货地址");
        return;
    }else if(addressId=="-1"){
        if(newAddress=="" || newAddress==null){
            alert("请填写新的收货地址");
            return;
        }else if(newAddress.length<=2 || newAddress.length>=50){
            alert("收货地址为2-50个字符");
            return;
        }
    }
    jq.ajax({
        url: contextPath + "/pre/CartServlet",
        method: "post",
        data: {
            action:"submit",
            addressId:addressId,
            newAddress:newAddress,
            newRemark:newRemark
        },
        dataType:"text",
		success:function(jsonStr){
			if(jsonStr=="订单详情已提交"){
				alert(jsonStr);
				window.location.href="CartServlet?action=Buy_Three";
			}else{
				alert(jsonStr);
			}
		}
    });
}